import logging

from airflow.models import Pool
from airflow.utils.session import provide_session

log = logging.getLogger(__name__)


@provide_session
def get_pools(page, session: None):
    if page == 0:
        roles = Pool.get_pools(session)
        map_roles = map(Pool.to_json, roles)
        return list(map_roles)
    else:
        role_result = Pool.get_pools_page(page, session)
        role_result.items = list(map(Pool.to_json, role_result.items))
        result = role_result.to_json()
        return result


@provide_session
def delete_pool(id, session: None):
    Pool.delete_pool(id, session)


def add_pool(name, slots, desc):
    pool = Pool(name, slots, desc)
    Pool.add_pool(pool)


@provide_session
def update_pool(id, name, slots, desc, session=None):
    pool = Pool(name, slots, desc)
    pool.id = id
    Pool.update_pool(pool, session)